//
//  YCRecruitViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/24.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  招募学员

#import "YCRecruitViewController.h"
#import "YCHadRecruitViewController.h"
#import "YCNavigationViewController.h"

@interface YCRecruitViewController ()
@property (nonatomic,strong) UITextField *nameText;
@property (nonatomic,strong) UITextField *idText;
@property (nonatomic,strong) UITextField *phoneText;

@property (nonatomic,strong) UIView *nameView;
@property (nonatomic,strong) UIView *idView;
@property (nonatomic,strong) UIView *phoneView;
@end

@implementation YCRecruitViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configView];
    [self yc_addBackBarButtonItem];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarResume];
    self.view.backgroundColor = [UIColor yc_colorWithRed:245 Green:245 Blue:245];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"已招募" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    self.navigationItem.rightBarButtonItem = item;
    self.navigationItem.title = @"招募学员";
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

#pragma mark -- 已招募

-(void)rightBtnClick
{
    YCHadRecruitViewController *hadVC = [[YCHadRecruitViewController alloc] init];
    [self.navigationController pushViewController:hadVC animated:NO];
}

-(void)configView
{
    CGFloat navH = 64;
    
    CGFloat IPHONE_HEIGHT = [UIScreen mainScreen].bounds.size.height;
    CGFloat IPHONE_WIDTH = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat itemW = 312;
    CGFloat itemH = 43;
    
    //name
    self.nameView = [[UIView alloc] initWithSize:CGSizeMake(itemW, itemH)];
    [self.nameView setPosition:CGPointMake(IPHONE_WIDTH * 0.5, 45) atAnchorPoint:CGPointMake(0.5, 0)];
    [self.view addSubview:self.nameView];
    self.nameView.backgroundColor = [UIColor whiteColor];
    
    
    //id
    self.idView = [[UIView alloc] initWithSize:self.nameView.yc_size];
    [self.idView setPosition:CGPointMake(self.nameView.yc_x, CGRectGetMaxY(self.nameView.frame) + 12) atAnchorPoint:CGPointZero];
    [self.view addSubview:self.idView];
    self.idView.backgroundColor = [UIColor whiteColor];
    
    
    //phone
    self.phoneView = [[UIView alloc] initWithSize:self.nameView.yc_size];
    [self.phoneView setPosition:CGPointMake(self.nameView.yc_x, CGRectGetMaxY(self.idView.frame) + 12) atAnchorPoint:CGPointZero];
    [self.view addSubview:self.phoneView];
    self.phoneView.backgroundColor = [UIColor whiteColor];
    
    
    //name
    self.nameText = [[UITextField alloc] initWithSize:CGSizeMake(self.nameView.yc_width - 28 , itemH)];
    [self.nameText setPosition:CGPointMake(14, self.nameView.yc_height * 0.5) atAnchorPoint:CGPointMake(0, 0.5)];
    self.nameText.backgroundColor = [UIColor whiteColor];
    [self.nameView addSubview:self.nameText];
    self.nameText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.nameText.layer.cornerRadius = 5;
    self.nameText.layer.masksToBounds = YES;
    self.nameText.font = [UIFont systemFontOfSize:13];
    self.nameText.placeholder = @"学员姓名";
    
    //id
    self.idText = [[UITextField alloc] initWithSize:self.nameText.yc_size];
    [self.idText setPosition:CGPointMake(self.nameText.yc_x, self.nameText.yc_y) atAnchorPoint:CGPointMake(0, 0)];
    self.idText.backgroundColor = [UIColor whiteColor];
    [self.idView addSubview:self.idText];
    self.idText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.idText.placeholder = @"身份证号码";
    self.idText.layer.cornerRadius = 5;
    self.idText.layer.masksToBounds = YES;
    self.idText.font = [UIFont systemFontOfSize:13];
    
    //phone
    self.phoneText = [[UITextField alloc] initWithSize:self.nameText.yc_size];
    [self.phoneText setPosition:CGPointMake(self.nameText.yc_x,self.nameText.yc_y) atAnchorPoint:CGPointMake(0, 0)];
    self.phoneText.backgroundColor = [UIColor whiteColor];
    [self.phoneView addSubview:self.phoneText];
    self.phoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneText.placeholder = @"手机号码";
    self.phoneText.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneText.layer.cornerRadius = 5;
    self.phoneText.layer.masksToBounds = YES;
    self.phoneText.font = [UIFont systemFontOfSize:13];
    
    UIButton *topUpBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    topUpBtn.yc_size = CGSizeMake(312, 34);
    [topUpBtn setPosition:CGPointMake(IPHONE_WIDTH * 0.5, IPHONE_HEIGHT - 54 - navH) atAnchorPoint:CGPointMake(0.5, 1)];
    [topUpBtn setTitle:@"确定提交" forState:UIControlStateNormal];
    [topUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    topUpBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    topUpBtn.backgroundColor = [UIColor yc_colorWithRed:55 Green:80 Blue:154];
    topUpBtn.layer.masksToBounds = YES;
    topUpBtn.layer.cornerRadius = 19;
    [topUpBtn addTarget:self action:@selector(topUpBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topUpBtn];
    
}

-(BOOL)checkIsValid
{
    NSString *name = [self.nameText.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *idText = [self.idText.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *phone = [self.phoneText.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (name.length == 0 || idText.length == 0 || phone.length == 0) {
        [self showMsg:@"每个选项都需要填写，不能为空"];
        return NO;
    }
    
    
    
    if (![self validateIdentityCard:idText]) {
        [self showMsg:@"请填写有效的身份证号码"];
        return  NO;
    }
    
    if (![self validateMobile:phone]) {
        [self showMsg:@"请填写有效的手机号码"];
        return NO;
    }
    
    return YES;
}

//手机号码验证
- (BOOL)validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//身份证号
- (BOOL)validateIdentityCard:(NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}


-(void)showMsg:(NSString *)msg
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
    [alertView show];
}


#pragma mark -- 确认提交
-(void)topUpBtnClicked:(UIButton *)btn
{
    
    if(![self checkIsValid])
    {
        return;
    }
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             @"ycname":self.nameText.text,
                             @"ycidcard":self.idText.text,
                             @"ycmobile":self.phoneText.text
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIRecruitSubmit params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        YCLog(@"提交成功");
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [SVProgressHUD showSuccessWithStatus:@"提交失败"];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
